package com.comp490.navigation;

import java.util.List;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

import com.comp490.shooter.R;
import com.comp490.shooter.ScoreConduit;
import com.comp490.shooter.ScoreStruct;

public class MainMenuHighScoresFrag extends Fragment
{

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup viewgroup,
			Bundle state)
	{

		return inflater.inflate(R.layout.mainmenu_highscorefrag, viewgroup,
				false);
	}

	@Override
	public void onActivityCreated(Bundle state)
	{
		super.onActivityCreated(state);

		Typeface photonica = Typeface.createFromAsset(this.getActivity()
				.getAssets(), "photonica_regular.ttf");

		TextView title = (TextView) getActivity().findViewById(
				R.id.highscore_screentitle);

		title.setTypeface(photonica);

		TableLayout highscorestable = (TableLayout) getActivity().findViewById(
				R.id.highscores_table);

		showHighScores(highscorestable);
	}

	@Override
	public void onPause()
	{
		super.onPause();
	}

	private void showHighScores(TableLayout highscorestable)
	{
		List<ScoreStruct> highscores = ScoreConduit.readHighScores(
				getActivity(), 1);
		Typeface arkitech = Typeface.createFromAsset(this.getActivity()
                .getAssets(), "arkitech_light.ttf");
		ScoreStruct score;
		for(int i = 0 ; i < highscores.size(); i++)
		{
			score = highscores.get(i);
			TableRow row = new TableRow(getActivity());
			if(i%2 != 0)
			{
				row.setBackgroundResource(R.color.DarkSlateGray);
			}
			row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT));

			TextView text = new TextView(getActivity());
			text.setTextAppearance(getActivity(), R.style.mainmenu_button);
			text.setGravity(Gravity.CENTER);
			text.setTypeface(arkitech);
			text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT));
			text.setText((i+1)+".");
			row.addView(text);
			
			text = new TextView(getActivity());
			text.setTextAppearance(getActivity(), R.style.mainmenu_button);
			text.setGravity(Gravity.CENTER);
			text.setTypeface(arkitech);
			text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT));
			text.setText(score.getName());
			row.addView(text);

			text = new TextView(getActivity());
			text.setTextAppearance(getActivity(), R.style.mainmenu_button);
			text.setGravity(Gravity.RIGHT);
			text.setTypeface(arkitech);
			text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT));
			text.setText(Long.toString(score.getScore()));
			row.addView(text);
			
			highscorestable.addView(row, new TableLayout.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); // maybe
																			// add
																			// params
		}

	}

}
